const { sequelize } = require('../config/database');
require('dotenv').config();

async function addUnreadListField() {
    try {
        console.log('开始为user_ai_conversations表添加last_message_status字段...');
        
        const queryInterface = sequelize.getQueryInterface();
        
        const tables = await queryInterface.showAllTables();
        if (!tables.includes('user_ai_conversations')) {
            console.log('user_ai_conversations表不存在，无法添加字段');
            return;
        }
        
        const tableDescription = await queryInterface.describeTable('user_ai_conversations');
        console.log('当前表结构:', Object.keys(tableDescription));
        
        if (tableDescription.last_message_status) {
            console.log('last_message_status字段已存在，无需添加');
            return;
        }
        
        console.log('添加last_message_status字段...');
        await queryInterface.addColumn('user_ai_conversations', 'last_message_status', {
            type: sequelize.Sequelize.INTEGER,
            allowNull: false,
            defaultValue: 0,
        });
        
        console.log('last_message_status字段添加成功！');
        
        const newTableDescription = await queryInterface.describeTable('user_ai_conversations');
        console.log('新表结构:', Object.keys(newTableDescription));
        
    } catch (error) {
        console.error('添加字段失败:', error);
        throw error;
    }
}

if (require.main === module) {
    (async () => {
        try {
            await addUnreadListField();
            console.log('字段添加成功完成');
            process.exit(0);
        } catch (error) {
            console.error('字段添加失败:', error);
            process.exit(1);
        }
    })();
}

module.exports = { addUnreadListField }; 